<link href="/static/layui/css/layui.css" rel="stylesheet">
<form class="layui-form" action="" id="add_option_from">

    <input name="id" style="display: none" id="id">

    <div class="layui-form-item">
        <label class="layui-form-label">题目名称</label>
        <div class="layui-input-inline">
            <input type="text" name="titleName" id="titleName" required  lay-verify="required" placeholder="请输入可选题目名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属专业</label>
        <div class="layui-input-inline">
            <input type="text" name="majorName" id="majorName" required lay-verify="required" placeholder="请输入题目所属专业" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">可选人数</label>
        <div class="layui-input-inline">
            <input type="text" name="totalCount" id="totalCount" required lay-verify="required" placeholder="请输入可选人数" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">题目详情</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入题目的详细信息..." class="layui-textarea" name="titleDetail"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <!--提交新增的按钮-->
            <button class="layui-btn" lay-submit lay-filter="submitAddOptional" id="submitAddOptional">立即提交</button>
            <!--提交修改的按钮-->
            <button class="layui-btn" style="display: none" lay-submit lay-filter="submitUpdateOptional" id="submitUpdateOptional">提交修改</button>
        </div>
    </div>
</form>
<script src="/static/js/jquery-3.3.1.min.js"></script>
<script src="/static/layui/layui.js" type="text/javascript"></script>
<script src="/static/js/ajax.js"></script>
<script>

    layui.use('form', function(){
        var form = layui.form;

        // 提交新增
        $("#submitAddOptional").click(function () {
            $.ajax({
                type: "POST",
                url: "/api/v1/teacher/title",
                dataType:"json",
                data: $("#add_option_from").serialize(),
                success:function (data) {
                    if(data.code==0 && data.msg=='success'){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }else{
                        layer.msg("新增可选题目失败")
                    }
                }
            });
            // 阻止layui弹出层页面刷新
            return false;
        });


        // 提交修改
        $("#submitUpdateOptional").click(function () {
            $.ajax({
                type: "Patch",
                url: "/api/v1/teacher/title",
                dataType:"json",
                data: $("#add_option_from").serialize(),
                success:function (data) {
                    if(data.code==0 && data.msg=='success'){
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }else{
                        layer.msg("修改可选题目失败")
                    }
                }
            });
            // 最值layui弹出层页面刷新
            return false;
        })
    });
</script>